//
//  WBComposeTextView.swift
//  Weibo
//
//  Created by xu on 2017/3/28.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

class WBComposeTextView: UITextView {
    
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    
    
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        backgroundColor = UIColor.white
        addSubview(placeholderLabel)
        placeholderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(ScreenWidth - 10)
        }
         //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(textViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    
    
    
    
    
    fileprivate lazy var placeholderLabel:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        return label
        
    }()
    
    
    //销毁通知
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}





extension WBComposeTextView{

    @objc fileprivate func textViewTextDidChange(){
        
    self.placeholderLabel.isHidden = self.hasText
    }


}
